package com.crm.service.crm.clue.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.model.common.dtos.PageResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.crm.clue.dtos.CluePoolDto;
import com.crm.model.crm.clue.pojos.Clue;
import com.crm.model.crm.clue.vos.CluePoolVo;
import com.crm.service.crm.clue.mapper.ClueSMapper;
import com.crm.service.crm.clue.service.CluePoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CluePoolServiceImpl implements CluePoolService {

    @Autowired
    private ClueSMapper clueMapper;

    // 测试写死数据
    Long userId = 2l; // 从线程中获取身份id todo:暂未实现
    String userName = "张三"; //拥有人姓名

    /**
     * 查询线索池以及高级条件搜索
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult cluePoolSearch(CluePoolDto dto) {
        // 分页效验     如果为空返或数据异常 默认返回 0 页    10 条
        dto.pageAndPageSizeNotNull();

        // 分页部分                         当前页         分页条数
        IPage<Clue> page = new Page<>(dto.getPage(), dto.getPageSize());

        // 动态条件
        LambdaQueryWrapper<Clue> lqe = new LambdaQueryWrapper<>();

        // 动态条件：手机号 | 线索id名字 | 创建时间 | 渠道来源
        lqe.like(dto.getPhoneNumber() != null, Clue::getPhoneNumber, dto.getPhoneNumber())
                .like(dto.getClueIdName() != null, Clue::getCueIdName,  dto.getClueIdName())
                //todo: 时间部分没有测试
                .between(dto.getCreateTimeSrting() != null, Clue::getCreateTime, dto.getStarOfDay(), dto.getEndOfDay())
                .eq(dto.getChannelSource() != null, Clue::getChannelSource, dto.getChannelSource());
        lqe.eq(Clue::getCuePoolStatus, 0); //代表：线索池  Todo:该常量需要归类

        // 分页查询
        clueMapper.selectPage(page, lqe);
        // 查询返回数据   原始数据 | Vo数据
        List<Clue> records = page.getRecords(); // 原始数据
        List<CluePoolVo> vos = new ArrayList<>();
        for (Clue record : records) {
            CluePoolVo cluePoolVo = CluePoolVo.builder()
                    .clueIdName(record.getCueIdName())
                    .phoneNumber(record.getPhoneNumber())
                    .channelSource(record.getChannelSource())
                    .createDate(record.getCreateTime())
                    .operationStatus(record.getOperationStatus())
                    .nameActivity(record.getNameActivity())
                    .build();
            vos.add(cluePoolVo);
        }

        // 分页返回值                            当前数据    总条数
        PageResult pageResult = new PageResult(page.getTotal(), vos);

        // 将分页返回
        return ResponseResult.okResult(pageResult);
    }

    /**
     * 捞取线索
     *
     * @param clueIdName todo:这个部分需要查询用户拥有的线索数量，如果添加超过则返回可添加的线索数量
     * @return
     */
    @Override
    public ResponseResult getAClue(List<String> clueIdName) {

        if (clueIdName == null || clueIdName.size() == 0) {
            throw new RuntimeException("捞取数据为空");
        }

        LambdaQueryWrapper<Clue> lqw = new LambdaQueryWrapper<>();
        lqw.in(Clue::getCueIdName,clueIdName);

        Clue clue = new Clue();
        clue.setOwnerId(userId);
        clue.setCuePoolStatus((byte) 1);

        int update = clueMapper.update(clue, lqw);

        if (update == 0){
            throw new RuntimeException("未能成功添加数据");
        }

        return new ResponseResult();
    }
}
